package com.project.controller.web;

import com.alibaba.druid.util.StringUtils;
import com.project.common.utils.PublicUtils;
import com.project.entity.ExerKpoint;
import com.project.myBeans.R;
import com.project.service.ExerkpointService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@EnableAutoConfiguration
@RestController
@RequestMapping("/exerkpoint")
@Api(description = "题目知识点对应接口", value = "exerkpoint")
public class ExerkpointController {

    @Autowired
    ExerkpointService exerkpointService;

    /**
     * fyk 2018/4/16
     *
     * @return
     */
    @ApiOperation("添加题目知识点对应记录")
    @PostMapping("/add")
    public R add(@RequestBody ExerKpoint exerKpoint) {
        try {
            if (exerkpointService.add(exerKpoint) > 0) {
                return R.ok();
            } else {
                return R.error();
            }
        } catch (Exception e) {
            e.printStackTrace();
            return R.error();
        }
    }

    /**
     * author: fyk
     * time: 21:48 2018/4/24
     * function: addBatch
     *
     * @param exerInfoId
     * @param kpointIds
     * @return
     */
    @ApiOperation("通过题目id批量添加题目知识点对应记录")
    @GetMapping("/addBatch/{exerInfoId}/{kpointIds}")
    public R addBatch(@PathVariable Integer exerInfoId, @PathVariable String kpointIds) {
        try {
            List<Integer> kpointList = new ArrayList<>();
            if (!StringUtils.isEmpty(kpointIds)&&!kpointIds.equals("N"))
                kpointList = PublicUtils.getIntIdList(kpointIds, ",");
            exerkpointService.addBatch(exerInfoId, kpointList);
            return R.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return R.error();
        }
    }

    /**
     * author: fyk
     * time: 9:36 2018/5/3
     * function: findByExerInfoId
     * 
     * @param exerInfoId
     * @return
     */
    @ApiOperation("通过题目id返回知识点列表")
    @GetMapping("/findByExerInfoId/{exerInfoId}")
    public R findByExerInfoId(@PathVariable Integer exerInfoId)
    {
        try{
            return R.ok().put("data",exerkpointService.findByExerInfoId(exerInfoId));
        }catch (Exception e){
            e.printStackTrace();
            return R.error();
        }
    }
}
